<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\CrontabTaskModel;
use app\service\client\AccountService;
use app\service\CrontabTaskService;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UpdateAccountEndDateWork extends BaseWork implements DailyStatWork
{
    public ?string $tag = null;

    public function setTag($tag = null): bool
    {
        if (! is_null($tag)) {
            $this->tag = $tag;
        }
        return true;
    }

    public function getTag(): ?string
    {
        return strval($this->tag);
    }

    public function getCommandName(): string
    {
        return $this->getName();
    }

    /**
     * 获取定时任务.
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCrontabTaskInfo(): CrontabTaskModel
    {
        $where = [
            ['task_name', '=', $this->getName()],
            ['tag', '=', $this->getTag()],
        ];

        $obj = new CrontabTaskModel();
        $model = $obj->where($where)->field(true)->find();
        return $model instanceof CrontabTaskModel ? $model : $obj;
    }

    protected function configure()
    {
        // 指令配置
        $this->setName('update_account_end_date_work');
        // 设置参数
        $this->setDescription('更新账户投放结束日期')
//            ->addArgument('media_id', Argument::REQUIRED, '媒体id')
            ->setHelp('php think be_pull_cost_task_work [media_id]');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        //        $mediaId = intval($input->getArgument('media_id'));
        //        $this->setTag($mediaId);
        $output->writeln('=======更新账户投放结束日期开始' . date('Y-m-d H:i:s', time()) . '=======');
        // 获取所有有效账户
        try {
            $params = [
                //                'media_id' => $mediaId,
                'end_date' => intval(date('Ymd')),
            ];
            $accountIds = AccountService::getTimeAccount($params);
            if (! empty($accountIds)) {
                foreach ($accountIds as $accountModel) {
                    $res = AccountService::updateTimeAccount($accountModel, (int) date('Ymd', strtotime('+150 days')));
                    if (! $res) {
                        $output->writeln('账户:' . $accountModel->getAttr('advertiser_id') . AccountService::getError(0) . date('Y-m-d H:i:s', time()) . '=======');
                    }
                }
            }
            // 昨日更新任务执行时间
            CrontabTaskService::setSingleTask($this);
        } catch (\Exception $e) {
            $output->writeln($e->getMessage() . date('Y-m-d H:i:s', time()));
        }
        $output->writeln('=======更新账户投放结束日期结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
